package com.ebelter.btcomlib.models.bluetooth.products.temperature;



import android.content.Context;
import android.text.TextUtils;

import com.ebelter.btcomlib.models.bluetooth.base.BaseManager;

import com.ebelter.btcomlib.models.bluetooth.bean.ProductStyle;
import com.ebelter.btcomlib.models.bluetooth.bean.SendMessage;
import com.ebelter.btcomlib.models.bluetooth.interfaces.IDataReadWriteCallback;
import com.ebelter.btcomlib.models.bluetooth.products.temperature.interfaces.I_T_MeasureCallback;
import com.ebelter.btcomlib.utils.BytesUtils;
import com.ebelter.btcomlib.utils.SpUtil;
import com.ebelter.btcomlib.utils.log.LogUtils;

/**
 * 温度贴
 */
public class TemperatureManager extends BaseManager {
    private static final String TAG = "TemperatureManager";




    private TemperatureBytesMakeFatory mTemperatureBytesMakeFatory;//制包工具类

    private TemperatureBytesAnalysisFatory mTemperatureBytesAnalysisFatory;//解包工具类


    public TemperatureManager(Context mContext) {

        super(mContext, ProductStyle.TEMPERATURE,TemperatureConstant.T_SERVICE_UUID,
                new String[]{TemperatureConstant.T_WRITE_UUID},new String[]{TemperatureConstant.T_READ_UUID},true,300);


        mTemperatureBytesAnalysisFatory = new TemperatureBytesAnalysisFatory();

        mTemperatureBytesMakeFatory = new TemperatureBytesMakeFatory();





        mServiceGatt.setIDataReadWriteCallback(new IDataReadWriteCallback() {
            @Override
            public void onCharacteristicChanged(byte[] data) {
                LogUtils.i(TAG,"TemperatureManager--IDataReadWriteCallback-onCharacteristicChanged--读到-"+ BytesUtils.bytes2HexStr(data));
                mTemperatureBytesAnalysisFatory.resultAnalysis(data);
            }

            @Override
            public void writeData(byte[] data) {
                // LogUtils.i(TAG,"ScaleBlue--IDataReadWriteCallback-writeData--写入-"+ Arrays.toString(data));
            }
        });
    }




    //设置测量状态监听
    public void setmIMeasureResultCallback(I_T_MeasureCallback mI_T_MeasureCallback) {
        mTemperatureBytesAnalysisFatory.setmIMeasureResultCallback(mI_T_MeasureCallback);
    }






    public static boolean isBondDevice(){
        String add=getBondDeviceAddress();
        return !TextUtils.isEmpty(add);
    }

    public static String getBondDeviceAddress(){
        return SpUtil.readString(TemperatureConstant.T_BOND_DEVICE_ADDRESS,null);
    }

    public static String getBondDeviceName(){
        return SpUtil.readString(TemperatureConstant.T_BOND_DEVICE_NAME,null);
    }

    public static void setBondDeviceAddress(String address){
        SpUtil.writeString(TemperatureConstant.T_BOND_DEVICE_ADDRESS,address);
    }

    public static void setBondDeviceName(String name){
        SpUtil.writeString(TemperatureConstant.T_BOND_DEVICE_NAME,name);
    }



    //同步时间
    public void syncSystemClock(){
        if (mTemperatureBytesMakeFatory!=null){
            SendMessage msg = mTemperatureBytesMakeFatory.getSyncSystemClock();
            addSendMsg(msg);
        }
    }


    public void getHistory(){
        if (mTemperatureBytesMakeFatory!=null){
            SendMessage msg = mTemperatureBytesMakeFatory.getHistory();
            addSendMsg(msg);
        }
    }




}
